//
//  DDRootviewTableViewCell.m
//  ialone
//
//  Created by 陆翀 on 16/1/11.
//  Copyright © 2016年 JasperSong. All rights reserved.
//

#import "DDRootviewTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "DDMusicPlayer.h"
#import "JRPlayerViewController.h"

@interface DDRootviewTableViewCell ()

@property (nonatomic, strong) UIView *playMusicBar;  // 音乐播放栏
@property (nonatomic, strong) UIButton *playButton;  // 播放按钮
@property (nonatomic, strong) UIButton *forwardButton;  // 快进按钮 (10s)
@property (nonatomic, strong) UIButton *toBackButton;  // 回退(10s)
@property (nonatomic, strong) UISlider *slider;  // 播放的进度
@property (nonatomic, strong) UILabel *endTimeLabel;  // 还需要播放多少时间
@property (nonatomic, strong) UILabel *currentTimeLabel; // 已经播放的时间

// 音频播放器
@property (nonatomic, strong) DDMusicPlayer *musicPlayer;
@property (nonatomic, strong) UIViewController *vc;


@end

@implementation DDRootviewTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _vc = [[UIViewController alloc]init];
        [self.contentView addSubview:_vc.view];
        [self.contentView sendSubviewToBack:_vc.view];
        
        // 图片
        _imagev = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight*2/5)];
        // 上面添加控件 要开交互
        _imagev.userInteractionEnabled = YES;
        [self.contentView addSubview:_imagev];
        
        // 显示类型的毛玻璃原型图片
        _contentType = [[DDContentType alloc]initWithFrame:CGRectMake(kScreenWidth/2 - 40, _imagev.bottom - 140, 80, 80)];
        [self.contentView addSubview:_contentType];
        
        // 分割线
        _line = [[UILabel alloc]initWithFrame:CGRectMake(0, _imagev.bottom, kScreenWidth, 2)];
        _line.backgroundColor = [UIColor brownColor];
        [self.contentView addSubview:_line];
        
        // 分类标签
        _category = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth - 10*kLMarge)/2, _line.bottom, 10 * kLMarge,2* kLMarge)];
        _category.backgroundColor = [UIColor brownColor];
        _category.textColor = [UIColor whiteColor];
        _category.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_category];
        
        // 题目标签
        _title = [[UILabel alloc]initWithFrame:CGRectMake(0, _category.bottom + 3 * kLMarge, kScreenWidth, 4 * kLMarge)];
        _title.font = [UIFont systemFontOfSize:33];
        _title.numberOfLines = 0;
        _title.textAlignment = NSTextAlignmentCenter;
        _title.numberOfLines = -1;
        [self.contentView addSubview:_title];
        
        _excerpt = [[UILabel alloc]initWithFrame:CGRectMake(3 * kLMarge, _category.bottom + 80, kScreenWidth - 6*kLMarge, kScreenHeight/5*1.5)];
        _excerpt.numberOfLines = -1;
        _excerpt.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_excerpt];
        
        
        // 下分割线
        _lineshort = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/5,kScreenHeight*5/6,kScreenWidth*3/5,1 )];
        _lineshort.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_lineshort];
        
        
        // 作者
        _author = [[UILabel alloc]initWithFrame:CGRectMake(_lineshort.left, _lineshort.bottom + 2 * kLMarge, _lineshort.width ,30)];
        _author.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_author];
        
        
        // 评论人数
        _commentimage = [[UIImageView alloc]initWithFrame:CGRectMake(20, kScreenHeight-40, 20, 20)];
        _commentimage.image = [UIImage imageNamed:@"taolun"];
        [self.contentView addSubview:_commentimage];
        
        _comment = [[UILabel alloc]initWithFrame:CGRectMake(_commentimage.right + 10, kScreenHeight-40, 50, 20)];
        [self.contentView addSubview:_comment];
        
        // 喜欢人数
        _goodimage = [[UIImageView alloc]initWithFrame:CGRectMake(_comment.right + 10, kScreenHeight-40, 20, 20)];
        _goodimage.image = [UIImage imageNamed:@"shoucang"];
        [self.contentView addSubview:_goodimage];
        
        _good = [[UILabel alloc]initWithFrame:CGRectMake(_goodimage.right + 10, _goodimage.top, 50, 20)];
        [self.contentView addSubview:_good];
        
        // 阅读人数
        _view = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-150, kScreenHeight-40, 130, 20)];
        [self.contentView addSubview:_view];
        
        
        [self addMusicPlayer];
        
    }
    return self;
}

- (void)addMusicPlayer{
    // 播放音频的控件
    _playMusicBar = [[UIView alloc]initWithFrame:CGRectMake(self.imagev.left, self.imagev.bottom, self.imagev.width, self.imagev.height/3)];
    _playMusicBar.backgroundColor = [UIColor lightGrayColor];
    _imagev.clipsToBounds = YES;
    [_imagev addSubview:_playMusicBar];
    
    _slider = [[UISlider alloc]initWithFrame:CGRectMake(0,0, _playMusicBar.width, 2)];
    // 禁止用手拖动
    _slider.enabled = NO;
    [_slider setThumbImage:[UIImage imageNamed:@"slider"] forState:(UIControlStateNormal)];
    [_slider setThumbImage:[UIImage imageNamed:@"slider"] forState:(UIControlStateHighlighted)];
    [_playMusicBar addSubview:_slider];
    
    _playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _playButton.frame = CGRectMake(kScreenWidth/2 - 30,_playMusicBar.height/2 - 30 ,60 , 60);
    _playButton.selected = YES;
    // 正常状态是暂停状态 图标是播放  选中状态是播放状态 图标是暂停图标
    [_playButton setImage:[UIImage imageNamed:@"pause"] forState:(UIControlStateSelected)];
    [_playButton setImage:[UIImage imageNamed:@"towatch"] forState:(UIControlStateNormal)];
    [_playButton addTarget:self action:@selector(playOrPauser:) forControlEvents:(UIControlEventTouchUpInside)];
    [_playMusicBar addSubview:_playButton];
    
    _currentTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _slider.bottom, 50, 10)];
    _currentTimeLabel.font = [UIFont systemFontOfSize:9];
    _currentTimeLabel.textAlignment = NSTextAlignmentLeft;
    [_playMusicBar addSubview:_currentTimeLabel];
    
    _endTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 50, _slider.bottom, 50, 10)];
    _endTimeLabel.font = [UIFont systemFontOfSize:9];
    _endTimeLabel.textAlignment = NSTextAlignmentRight;
    [_playMusicBar addSubview:_endTimeLabel];
    
    _toBackButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _toBackButton.frame = CGRectMake(kScreenWidth/4 - 30, _playButton.top, _playButton.width, _playButton.height);
    [_toBackButton addTarget:self action:@selector(toBack:) forControlEvents:(UIControlEventTouchUpInside)];
    [_toBackButton setImage:[UIImage imageNamed:@"toback"] forState:(UIControlStateNormal)];
    [_playMusicBar addSubview:_toBackButton];
    
    _forwardButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _forwardButton.frame = CGRectMake(kScreenWidth/4*3 - 30, _playButton.top, _playButton.width, _playButton.height);
    [_forwardButton addTarget:self action:@selector(forward:) forControlEvents:(UIControlEventTouchUpInside)];
    [_forwardButton setImage:[UIImage imageNamed:@"forward"] forState:(UIControlStateNormal)];
    [_playMusicBar addSubview:_forwardButton];

    
}


-(void)setModel:(DDRootModel *)model
{
    _model = model;
    if ([model.title containsString:@"\n"]){
        _title.frame = CGRectMake(0, _category.bottom + 3 * kLMarge, kScreenWidth, 8 * kLMarge);
    }
    _title.text = model.title;
    _category.text = model.category;
    _comment.text = model.comment;
    _good.text = model.good;
    _view.text = [NSString stringWithFormat:@"阅读数:%@",model.view];
    _excerpt.text = model.excerpt;
    _author.text =model.author;
    
   
    
    // 设置图片
    [_imagev sd_setImageWithURL:[NSURL URLWithString:model.thumbnail]];
    
    
    if ([_category.text isEqualToString:@"TO READ"]){
        self.contentType.hidden = YES;
    }else if ([_category.text isEqualToString:@"TO LISTEN"]){
        self.contentType.hidden = NO;
        
        // 设置音频的来源
        _musicPlayer = [DDMusicPlayer shareMusicPlayerWithURLString:model.fm];
        [self.contentType.button removeTarget:self action:@selector(playVideo:) forControlEvents:(UIControlEventTouchUpInside) ];
        [self.contentType.button setImage:[UIImage imageNamed:@"tolisten"] forState:(UIControlStateNormal)];
        [self.contentType.button addTarget:self action:@selector(playMusic:) forControlEvents:(UIControlEventTouchUpInside)];
    }else{
        self.contentType.hidden = NO;
         [self.contentType.button removeTarget:self action:@selector(playMusic:) forControlEvents:(UIControlEventTouchUpInside) ];
        [self.contentType.button setImage:[UIImage imageNamed:@"towatch"] forState:(UIControlStateNormal)];
        [self.contentType.button addTarget:self action:@selector(playVideo:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    
}


#pragma mark ----- 按钮的响应
// 弹出音频的控件  并且播放音乐
- (void)playMusic:(UIButton *)button{
    _contentType.hidden = YES;
    [UIView animateWithDuration:0.5 animations:^{
        _playMusicBar.frame = CGRectMake(self.imagev.left, self.imagev.height/3 *2 , self.imagev.width, self.imagev.height/3);
    }];
    [_musicPlayer play];
    [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(playerPlaying) userInfo:nil repeats:YES];
}
- (void)playerPlaying{
    NSInteger currentTime = _musicPlayer.currentTime;
    NSInteger finishTime = _musicPlayer.finishTime;
    NSInteger endTime = finishTime - currentTime;
    _endTimeLabel.text  = [NSString stringWithFormat:@"%02ld:%02ld",endTime/60,endTime%60];
    _currentTimeLabel.text  = [NSString stringWithFormat:@"%02ld:%02ld",currentTime/60,currentTime%60];
    _slider.minimumValue = 0;
    _slider.maximumValue = finishTime;
    _slider.value = currentTime;
}
- (void)playOrPauser:(UIButton *)button{
    button.selected = !button.selected;
    if (button.selected){
        // 此时是暂停状态
        [_musicPlayer play];
    }else{
        [_musicPlayer pause];
    }
}
- (void)playVideo:(UIButton *)button{
    NSURL *url = [NSURL URLWithString:self.model.video];
    JRPlayerViewController *playerVC = [[JRPlayerViewController alloc] initWithHTTPLiveStreamingMediaURL:url];
    playerVC.mediaTitle = @"prog_index";
    [self.vc presentViewController:playerVC animated:NO completion:nil];
    
}

// 移出屏幕外的操作
- (void)repair{
    // 恢复原状
   _playMusicBar.frame = CGRectMake(0, self.imagev.bottom, self.imagev.width, self.imagev.height/3);
    // 显示出来
    _contentType.hidden = NO;
    // 停止播放器
    [_musicPlayer stop];
   
}
- (void)toBack:(UIButton *)button{
    [_musicPlayer toBack];
}
- (void)forward:(UIButton *)button{
    [_musicPlayer forward];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
